package com.mzj.saas.commons.pdf;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class JavaToPdfHtmlFreeMarker {
	private final static Logger logger = LoggerFactory.getLogger(JavaToPdfHtmlFreeMarker.class);
	private static Configuration freemarkerCfg = null;
	
	static {
		freemarkerCfg = new Configuration();
		// freemarker的模板目录
		try {
			String freeMarkDir = "/home/eagle/saas-service/contract/template/";
			freemarkerCfg.setDirectoryForTemplateLoading(new File(freeMarkDir));
			freemarkerCfg.setEncoding(Locale.CHINA, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 字体路径
	 * @return
	 * @author shun
	 * @date 2021年6月16日
	 */
	private static String getFontPath2() {
		return System.getProperty("user.dir") + "/src/main/resources/static/contract" + File.separator + "SIMHEI.TTF";
	}

	/**
	 * html转pdf
	 * @param content
	 * @param dest
	 * @throws IOException
	 * @throws DocumentException
	 * @throws com.lowagie.text.DocumentException
	 * @author shun
	 * @date 2021年6月16日
	 */
	public static void createPdf1(String content, String dest)
			throws IOException, DocumentException, com.lowagie.text.DocumentException {
		ITextRenderer render = new ITextRenderer();
		ITextFontResolver fontResolver = render.getFontResolver();
		fontResolver.addFont(getFontPath2(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		// 解析html生成pdf
		render.setDocumentFromString(content);
		// 解决图片相对路径的问题
		// render.getSharedContext().setBaseURL(LOGO_PATH);

		render.layout();
		render.createPDF(new FileOutputStream(dest));
	}

	/**
	 * freemarker渲染html
	 * 
	 * @param data
	 * @param htmlTmp
	 * @return
	 * @date 2021年6月9日
	 */
	public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
		Writer out = new StringWriter();
		try {
			// 获取模板,并设置编码方式
			Template template = freemarkerCfg.getTemplate(htmlTmp, "UTF-8");
			template.setEncoding("UTF-8");
			// 合并数据模型与模板
			// 将合并后的数据和模板写入到流中，这里使用的字符流
			template.process(data, out);
			out.flush();
			return out.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}

	public static void main(String[] args) throws IOException, DocumentException, InterruptedException {
		Map<String, Object> data = new HashMap();
		data.put("合同编号", "D20210511SDNQB");
		data.put("甲方姓名", "小哈");
		data.put("签字日期", "2021.05.13");
		String content = JavaToPdfHtmlFreeMarker.freeMarkerRender(data, "mode.html");
		
		try {
			createPdf1(content, "C:/Users/admin/Desktop/1.pdf");
		} catch (com.lowagie.text.DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}